class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum=0;
        for(int num:nums) sum+=num;
        int target=sum-x;
        if(target<0) return -1;
        int ans=0;
        for(int left=0,right=0,w=0;right<nums.size();++right)
        {
            w+=nums[right];
            while(left<=right&&w>target)
                w-=nums[left++];
            if(w==target) ans=max(ans,right-left+1);
        }
        return ans==0&&target!=0?-1:nums.size()-ans;
    }
};
